package util;

import java.util.List;
import java.util.Map;

import org.dom4j.Document;

import app.MConstants;

public class ModelToScriptUtil {
	public static String getFrameworkModeAndProjectModelScript(Map modelsMap) {
		//get framework model file path from properties
//		String modelFilePath = FileUtil.readValue(MConstants.FRAMEWORK_MODEL_FILE_PATH, MConstants.FRAMEWORK_MODEL);
		//get framework model file content
		String forameworkModelContent = FileUtil.getFileContentFromFilePath((String) modelsMap.get(MConstants.FRAMEWORK_MODEL));
		
		//get project model file path from properties
//		String projectFilePath = FileUtil.readValue(MConstants.PROJECT_MODEL_FILE_PATH, MConstants.PROJECT_MODEL);
		//get framework model file content
		String projectModelContent = FileUtil.getFileContentFromFilePath((String) modelsMap.get(MConstants.PROJECT_MODEL));

		String script = new XMLToMathUtil().getMathScriptFromXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>"+forameworkModelContent+projectModelContent+"</root>");
//		String packageString = "Needs[\"DBA`\", (NotebookDirectory[] <> \"DBA\\\\DBA.m\")]";
		String packageString = "Needs[\"DBA`\", (\"D:\\\\DBA.m\")]";
		return script.trim()+";\n"+packageString.trim();
	}
	
	public static String getProjectModelScript(String modelFilePath) {
		return null;
	}
	
	public static String getMethodModelScript(Map<String, String> modelsMap,String colNum) {
//		String modelFilePath = FileUtil.readValue(MConstants.METHOD_MODEL_FILE_PATH, MConstants.METHOD_MODEL);
		Document doc = XMLUtil.read((String)modelsMap.get(MConstants.METHOD_MODEL));
		List<String> methodList = XMLUtil.getMethodScript(doc);
		StringBuffer sb = new StringBuffer();
		int size = methodList.size();
		if(size<1)
		return "";
		if(size==1)
		return methodList.get(0).toString();
		else {
			int realColNum;
			if(!"".equals(colNum)) {
				realColNum = new Integer(colNum);
			} else {
				realColNum = MathUtil.getColNum(size);
			}
			sb.append("Grid[{{");
			int flag = 0;
			for(int i=0; i<size; i++) {
				++flag;
				if(flag==1) {
					sb.append(methodList.get(i).toString());
					if(flag==realColNum) {
						flag = 0;
						if(i!=size-1)
							sb.append("},{");
					}
				}
					
				else if(flag < realColNum) {
					sb.append(",");
					sb.append(methodList.get(i).toString());
				}
				else if(flag==realColNum) {
					if(i==(size-1)) {
						sb.append(",");
						sb.append(methodList.get(i).toString());
					} else {
						sb.append(",");
						sb.append(methodList.get(i).toString());
						sb.append("},{");
						flag = 0;
					}
				}
			}
			sb.append("}}]");
		}
		return sb.toString();
	}
	
}
